<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Catalog
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Product description block
 *
 * @category   Mage
 * @package    Mage_Catalog
 * @author     Magento Core Team <core@magentocommerce.com>
 */
class Mage_Catalog_Block_Product_View_review extends Mage_Catalog_Block_Product_Abstract 
{
    protected $_product = null;
    public function getProduct()
    {
//       if (!Mage::registry('product') && $this->getProductId()) {
//            $product = Mage::getModel('catalog/product')->load($this->getProductId());
//            Mage::register('product', $product);
//        }
//        return Mage::registry('product');
          if (!($this->getData('product') instanceof Mage_Catalog_Model_Product)) {
            $this->setData('product', Mage::registry('product'));
        }
        return $this->getData('product');
       /* if (!$this->_product) {
           $this->_product = Mage::registry('product');
        
        }
        return $this->_product; */
    }
      public function getToolbarHtml()
    {
        return $this->getChildHtml('toolbar');
    }
     public function productAttribute($product, $attributeHtml, $attributeName)
    {   
        $attribute = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeName);
        if ($attribute && $attribute->getId() && ($attribute->getFrontendInput() != 'media_image')
            && (!$attribute->getIsHtmlAllowedOnFront() && !$attribute->getIsWysiwygEnabled())) {
                if ($attribute->getFrontendInput() != 'price') {
                    $attributeHtml = $this->escapeHtml($attributeHtml);
                }
                if ($attribute->getFrontendInput() == 'textarea') {
                    $attributeHtml = nl2br($attributeHtml);
                }
        }
        if ($attribute->getIsHtmlAllowedOnFront() && $attribute->getIsWysiwygEnabled()) {
            if (Mage::helper('catalog')->isUrlDirectivesParsingAllowed()) {
                $attributeHtml = $this->_getTemplateProcessor()->filter($attributeHtml);
            }
        }

        $attributeHtml = $this->process('productAttribute', $attributeHtml, array(
            'product'   => $product,
            'attribute' => $attributeName
        ));

        return $attributeHtml;
    }

  
    public function getdataproductview($productid)
    {
          $resource = Mage::getSingleton('core/resource');
               $readConnection = $resource->getConnection('core_read');
               $table = $resource->getTableName('catalog/product');
               $table_review_detail= $resource->getTableName('review/review_detail');//get title,detail,nickname, customerid
               $table_review= $resource->getTableName('review/review');//get created on // review_id
               $table_review_status= $resource->getTableName('review/review_status');//get status_code
               $table_review_entity=$resource->getTableName('review/review_entity');//get entity_id, entity_code
               $table_review_store = $resource->getTableName('review/review_store');//get review_id, store_ID
              // $productId = $this->getRequest()->getParam('id');
                             
                //get name
               
                $model = Mage::getModel('catalog/product'); //getting product model
                $_product = $model->load($productid); //getting product object for particular product id
//            echo "product name : ";
//            echo $_product->getName().'<br/>'; //product name
                //SELECT p.sku FROM `catalog_product_entity` p join `review`  r on r.entity_pk_value = p.entity_id WHERE p.entity_id = 1
                //SELECT p.sku FROM `catalog_product_entity` p join `review`  r on r.entity_pk_value = p.entity_id ,`review` ri join `review_detail` rdt on ri.entity_id = rdt.review_id WHERE p.entity_id = 1
                 $query = 'SELECT count(r.review_id), r.review_id,r.created_at,rdt.title,rdt.customer_id, rdt.detail, rdt.nickname,rs.status_code, p.sku FROM ' . $table_review . ' r join ' .$table_review_store .
                  ' rvs on r.review_id = rvs.review_id  join ' .$table_review_detail. ' rdt on r.review_id = rdt.review_id join review_status rs on r.status_id = rs.status_id join '.$table.' p on r.entity_pk_value = p.entity_id '                  
                  . ' WHERE p.entity_id = '. (int)$productid 
                  .' Group By r.review_id'
                  .' Order By r.Review_ID DESC LIMIT 0 , 20 '
             ;
                
//            $data = Mage::getSingleton('core/resource') ->getConnection('core_read')->mysql_num_rows($query); 
//            echo 'row :'. $data;
//            while($row = mysql_fetch_array($data)){
//                echo "There are ". $row['COUNT(value_id)'] ." ". $row['value'] ." items.";
//                    echo "<br />";
//                } 
//                 $result = $readConnection->mysql_query($query) or die(mysql_error());
//              $total_row=mysql_num_rows($result);
//                 echo '$total_row :'. $total_row ;
             $sku = $readConnection->fetchAll($query);
          
           return $sku;
    }
    public function getnameproduct($productid)
    {
        $model = Mage::getModel('catalog/product');
        $_productname = $model->load($productid);
        return $_productname;
    }
    public function load_dataAction()
    {
        if($_POST['page'])
        {
        $page = $_POST['page'];
        $cur_page = $page;
        $page -= 1;
        $per_page = 15; // Per page records
        $previous_btn = true;
        $next_btn = true;
        $first_btn = true;
        $last_btn = true;
        $start = $page * $per_page;
        $productId = $this->getRequest()->getParam('id');
        $model = Mage::getModel('catalog/product'); //getting product model
                        $_product = $model->load($productid); //getting product object for particular product id
          $resource = Mage::getSingleton('core/resource');
                       $readConnection = $resource->getConnection('core_read');
                       $table = $resource->getTableName('catalog/product');
                       $table_review_detail= $resource->getTableName('review/review_detail');//get title,detail,nickname, customerid
                       $table_review= $resource->getTableName('review/review');//get created on // review_id
                       $table_review_status= $resource->getTableName('review/review_status');//get status_code
                       $table_review_entity=$resource->getTableName('review/review_entity');//get entity_id, entity_code
                       $table_review_store = $resource->getTableName('review/review_store');//get review_id, store_ID
                      // $productId = $this->getRequest()->getParam('id');    

         $query = "SELECT count(r.review_id), r.review_id,r.created_at,rdt.title,rdt.customer_id, rdt.detail, rdt.nickname,rs.status_code, p.sku FROM " . $table_review . " r join " .$table_review_store .
                          " rvs on r.review_id = rvs.review_id  join " .$table_review_detail. " rdt on r.review_id = rdt.review_id join review_status rs on r.status_id = rs.status_id join ".$table." p on r.entity_pk_value = p.entity_id "                
                          . " WHERE p.entity_id = ". (int)$productid 
                          ." Group By r.review_id "
                          . " Order By r.Review_ID DESC "
                           ." LIMIT ". "$start, $per_page";
                     ;
                     $getname =$_product->getName();
         $sku = $readConnection->fetchAll($query);
        //$result_pag_data = mysql_query($query) or die('MySql Error' . mysql_error());
        $msg = "";
        $count=0;
        foreach ($sku as $row)
        {
            $count=$count+1;
             if($row['customer_id'] == null || $row['customer_id']=='')
             {
                 $type ='Guest';
             }
             else
             {
                 $type ='Customer';
             }
             if($item['customer_id']=='2')
             {
                 $type ='Admin';
             }
        //$htmlmsg=htmlentities($row['message']); //HTML entries filter
        $msg .= "<tr class='even pointer' style='background-color: #F2F2F2;border: 1px solid #303030' >" 
        ."<tr class='even pointer' style='background-color: #F2F2F2;border: 1px solid #303030' >"
        ."<td class='a-center' style='padding:3px;border: 1px solid #BDBDBD;border-left: 0px;border-right: 0px'> '".htmlentities($row['review_id']). "'</td>"
        ."<td class='a-left' style='padding:3px;border: 1px solid #BDBDBD;border-left: 0px;border-right: 0px'> '".htmlentities($row['created_at']). "'</td>"
        ."<td class='a-center' style='padding:3px;border: 1px solid #BDBDBD;border-left: 0px;border-right: 0px'> '".htmlentities($row['status_code']). "'</td>"
        ."<td class='a-left' style='padding:3px;border: 1px solid #BDBDBD;border-left: 0px;border-right: 0px'> '".htmlentities($row['title']). "'</td>"
        ."<td class='a-left' style='padding:3px;border: 1px solid #BDBDBD;border-left: 0px;border-right: 0px'> '".htmlentities($row['nickname']). "'</td>"
        ."<td class='a-left' style='padding:3px;border: 1px solid #BDBDBD;border-left: 0px;border-right: 0px'> '".htmlentities($row['detail']). "'</td>"
        ."<td class='a-left' style='padding:3px;border: 1px solid #BDBDBD;border-left: 0px;border-right: 0px'> '".htmlentities($type). "'</td>"
        ."<td class='a-left' style='padding:3px;border: 1px solid #BDBDBD;border-left: 0px;border-right: 0px'> '".htmlentities($getname). "'</td>"
        ."<td class='a-left' style='padding:3px;border: 1px solid #BDBDBD;border-left: 0px;border-right: 0px'> '".htmlentities($row['sku']). "'</td>";
        }
        $msg="    <table id='productGrid_table' cellspacing='0' class='data' style='-moz-border-radius:10px;-webkit-border-radius:10px;-webkit-box-shadow: 5px 5px 2px #ccc;border: 2px solid #BDBDBD;border-top: 0.1px solid #BDBDBD;' >
                        <colgroup>
                                <col width='30'>
                                <col width='150'>
                                <col width='50'>
                                <col width='200'>
                                <col width='100'>
                                <col width='300'>
                                <col width='60'>
                                <col width='100'>
                        </colgroup>".

                "<thead style=''>
                            <tr style='background: #FFF9E9' class='headings'  >

                                <!--<th><span class='nobr'><a href='#' name='entity_id' title='asc' class='sort-arrow-desc'><span class='sort-title'>ID</span></a></span></th> -->
                                <th style='padding:5px;font-weight:bold;font-size: 15px;border: 1px solid #BDBDBD;border-left: 0px;-moz-border-radius:10px;'><span>ID</span></th> 
                                <th style='padding:5px;font-weight:bold;font-size: 15px;border: 1px solid #BDBDBD;border-left: 0px;-moz-border-radius:10px;-webkit-border-radius:10px;-webkit-box-shadow: 5px 5px 2px #ccc;    '><span class='nobr'><span class='sort-title'>Created On</span></span></th>
                                <th style='padding:5px;font-weight:bold;font-size: 15px;border: 1px solid #BDBDBD;border-left: 0px;-moz-border-radius:10px;-webkit-border-radius:10px; -webkit-box-shadow: 5px 5px 2px #ccc;   '><span class='nobr'><span class='sort-title'>Status</span></span></th>
                                <th style='padding:5px;font-weight:bold;font-size: 15px;border: 1px solid #BDBDBD;border-left: 0px;-moz-border-radius:10px;-webkit-border-radius:10px;  '><span class='nobr'><span class='sort-title'>Title</span></span></th>
                                <th style='padding:5px;font-weight:bold;font-size: 15px;border: 1px solid #BDBDBD;border-left: 0px;-moz-border-radius:10px;-webkit-border-radius:10px;  '><span class='nobr'><span class='sort-title'>Nickname</span></span></th>
                                <th style='padding:5px;font-weight:bold;font-size: 15px;border: 1px solid #BDBDBD;border-left: 0px;-moz-border-radius:10px;-webkit-border-radius:10px;  '><span class='nobr'><span class='sort-title'>Review</span></span></th>
                                <th style='padding:5px;font-weight:bold;font-size: 15px;border: 1px solid #BDBDBD;border-left: 0px;-moz-border-radius:10px;-webkit-border-radius:10px;  '><span class='nobr'><span class='sort-title'>Type</span></span></th>
                                <th style='padding:5px;font-weight:bold;font-size: 15px;border: 1px solid #BDBDBD;border-left: 0px;-moz-border-radius:10px;-webkit-border-radius:10px;  '><span class='nobr'><span class='sort-title'>Product Name</span></span></th>
                                <th style='padding:5px;font-weight:bold;font-size: 15px;border: 1px solid #BDBDBD;border-left: 0px;-moz-border-radius:10px;-webkit-border-radius:10px;  '><span class='nobr'><span class='sort-title'>Product SKU</span></span></th>
                             </tr>
                        </thead>".

                " <tbody style='border: 1px solid #BDBDBD'>".$msg."  </tbody>
            </table>";
        //$msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data
        /* -----Total count--- */
        //$query_pag_num = "SELECT COUNT(*) AS count FROM messages"; // Total records
        //$result_pag_num = mysql_query($query_pag_num);
        //$row = mysql_fetch_array($result_pag_num);
        //$count = $row['count'];
        $no_of_paginations = ceil($count / $per_page);
        /* -----Calculating the starting and endign values for the loop----- */

        //Some Code. Available in download script

        }
        $no_of_paginations = ceil($count / $per_page);

        /* ---------------Calculating the starting and endign values for the loop----------------------------------- */
        if ($cur_page >= 7) {
            $start_loop = $cur_page - 3;
            if ($no_of_paginations > $cur_page + 3)
                $end_loop = $cur_page + 3;
            else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) {
                $start_loop = $no_of_paginations - 6;
                $end_loop = $no_of_paginations;
            } else {
                $end_loop = $no_of_paginations;
            }
        } else {
            $start_loop = 1;
            if ($no_of_paginations > 7)
                $end_loop = 7;
            else
                $end_loop = $no_of_paginations;
        }
        /* ----------------------------------------------------------------------------------------------------------- */
        $msg .= "<div class='pagination'><ul>";

        // FOR ENABLING THE FIRST BUTTON
        if ($first_btn && $cur_page > 1) {
            $msg .= "<li p='1' class='active'>First</li>";
        } else if ($first_btn) {
            $msg .= "<li p='1' class='inactive'>First</li>";
        }

        // FOR ENABLING THE PREVIOUS BUTTON
        if ($previous_btn && $cur_page > 1) {
            $pre = $cur_page - 1;
            $msg .= "<li p='$pre' class='active'>Previous</li>";
        } else if ($previous_btn) {
            $msg .= "<li class='inactive'>Previous</li>";
        }
        for ($i = $start_loop; $i <= $end_loop; $i++) {

            if ($cur_page == $i)
                $msg .= "<li p='$i' style='color:#fff;background-color:#006699;' class='active'>{$i}</li>";
            else
                $msg .= "<li p='$i' class='active'>{$i}</li>";
        }

        // TO ENABLE THE NEXT BUTTON
        if ($next_btn && $cur_page < $no_of_paginations) {
            $nex = $cur_page + 1;
            $msg .= "<li p='$nex' class='active'>Next</li>";
        } else if ($next_btn) {
            $msg .= "<li class='inactive'>Next</li>";
        }

        // TO ENABLE THE END BUTTON
        if ($last_btn && $cur_page < $no_of_paginations) {
            $msg .= "<li p='$no_of_paginations' class='active'>Last</li>";
        } else if ($last_btn) {
            $msg .= "<li p='$no_of_paginations' class='inactive'>Last</li>";
        }
        $goto = "<input type='text' class='goto' size='1' style='margin-top:-1px;margin-left:60px;'/><input type='button' id='go_btn' class='go_button' value='Go'/>";
        $total_string = "<span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span>";
        $msg = $msg . "</ul>" . $goto . $total_string . "</div>";  // Content for pagination
        echo $msg;
            }

}


